// // 定义一个loading
// const loading = ref(false)

// // 点击关注或取消关注
// const likeOrdislike = async (doctor: Doctor) => {
//   loading.value = true
//   try {
//     await likeOrdislikeAPI(doctor.id)
//     // 异或表达式
//     doctor.likeFlag ^= 1
//   } finally {
//     loading.value = false
//   }
// }

import { onMounted, onUnmounted, ref, type Ref } from 'vue'
import type { ConsultOrderItem, LikeFlag } from '@/types/consult'
import {
  canelOrderAPI,
  delOrderAPI,
  getMedicalOrderDetailAPI,
  getMobileCode,
  getPrescriptionAPI,
  likeOrdislikeAPI
} from '@/services'
import { showImagePreview, showToast, type FormInstance } from 'vant'
import { OrderType } from '@/enum'
import type { OrderDetail } from '@/types/order'
import type { Type } from '@/types/user'
type Item = {
  id: string
  likeFlag: 0 | 1
}

// 创建一个函数
export const useFollow = (type: LikeFlag = 'doc') => {
  const loading = ref(false)
  // 关注方法  需要点击项的ID和关注状态
  const follow = async (item: Item) => {
    try {
      loading.value = true
      // 调用接口
      await likeOrdislikeAPI(item.id, type)
      // 改变关注的视图转态
      item.likeFlag ^= 1 // 异或
    } finally {
      loading.value = false
    }
  }
  return {
    loading,
    follow
  }
}

export const usePrescription = () => {
  // 点击原始处方，预览处方
  const showPrescription = async (id?: string) => {
    // 类型守卫
    if (id) {
      // 获取
      const res = await getPrescriptionAPI(id)
      // 预览
      showImagePreview([res.data.url])
    }
  }
  return {
    showPrescription
  }
}

// 取消订单
export const useCanelOrder = () => {
  // 取消问诊订单操作
  const loading = ref(false)
  const canelOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      // 调用取消接口
      await canelOrderAPI(item.id)
      // 改变订单的状态
      item.status = OrderType.ConsultCancel
      item.statusValue = '已取消'
      // 给个提示
      showToast('取消成功')
    } finally {
      loading.value = false
    }
  }
  return {
    loading,
    canelOrder
  }
}

// 删除订单
export const useDelOrder = (cb?: () => void) => {
  //接受一个回调函数
  // 删除的loading效果
  const loading = ref(false)
  const delOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      // 调用删除接口
      await delOrderAPI(item.id)
      // emit('delOrder', item.id)  这里逻辑不同，需要传入参数，让用户决定做什么

      cb?.() //？.cb有值再调用执行
      // 给出提示
      showToast('删除成功')
    } catch (err) {
      console.log(err)
    } finally {
      loading.value = false
    }
  }
  return {
    loading,
    delOrder
  }
}

// 获取药品订单详情
export const useOrderDetail = (id: string) => {
  // 获取药品订单详情
  // 定义药品订单详情数据
  const orderDetail = ref<OrderDetail>()
  onMounted(async () => {
    const res = await getMedicalOrderDetailAPI(id)
    orderDetail.value = res.data
  })
  return {
    orderDetail
  }
}

// 获取短信验证码
export const useCodeByMobile = (mobile: Ref<string>, type: Type) => {
  const refForm = ref<FormInstance | null>(null) //给表单绑定ref属性
  const time = ref(0) //定时时间
  let timeId: number //定义计时器
  // 发送短信验证码
  const sendCode = async () => {
    try {
      if (time.value > 0) return //如果有定时器，就不能再继续点击发送
      // 校验手机号   validate(),需要校验单个属性，就传入name属性，没传就是校验表单所有的值
      await refForm.value?.validate('mobile')
      console.log('校验通过')
      await getMobileCode(mobile.value, type)
      time.value = 60 //倒计时60秒
      timeId = setInterval(() => {
        time.value--
        if (time.value < 0) {
          // 如果时间到0 倒计时走完，就关闭定时器
          clearInterval(timeId)
          // 将定时器归0
          time.value = 0
        }
      }, 1000)
    } catch (err) {
      console.log(err)
    }
  }
  // 组件卸载时，清除定时器
  onUnmounted(() => {
    clearInterval(timeId)
  })

  return {
    sendCode,
    time,
    refForm
  }
}
